package de.osmembrane.source_file_header;

import java.io.File;
import java.util.ArrayList;
import java.util.List;
import java.util.regex.Pattern;

/**
 * Reads the files recursively.
 * 
 * @author jakob_jarosch
 */
public class Filer {

	private File folder;
	private Pattern filter;
	private List<File> files = new ArrayList<File>();
	
	public Filer(File folder) {
		this.folder = folder;
	}
	
	public File[] searchRecursivly(String filter) {
		this.filter = Pattern.compile(filter);
		
		searchRecursivly(folder);
		
		return files.toArray(new File[files.size()]);
	}
	
	private void searchRecursivly(File folder) {
		if(folder.isDirectory()) {
			for(File file : folder.listFiles()) {
				searchRecursivly(file);
			}
		} else if (folder.isFile()) {
			//System.out.println("Found a file: " + folder.getAbsolutePath());
			if(filter.matcher(folder.getAbsolutePath()).find() && !folder.getAbsolutePath().contains(".svn")) {
				//System.out.println("> File matched.");
				files.add(folder);
			}
		}
	}
}
